import { defineStore } from 'pinia'
import { store } from '../index'
// @ts-ignore
import { DictDataVO } from '@/api/system/dict/types'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache('sessionStorage')
import { getSimpleDictDataList } from '@/api/system/dict/dict.data'

export interface DictValueType {
  value: any
  label: string
  clorType?: string
  cssClass?: string
}
export interface DictTypeType {
  dictType: string
  dictValue: DictValueType[]
}
export interface DictState {
  dictMap: Map<string, any>
  isSetDict: boolean
}

export const useDictStore = defineStore('dict', {
  state: (): DictState => ({
    dictMap: new Map<string, any>(),
    isSetDict: false
  }),
  getters: {
    getDictMap(): Recordable {
      const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
      if (dictMap) {
        this.dictMap = dictMap
      }
      return this.dictMap
    },
    getIsSetDict(): boolean {
      return this.isSetDict
    }
  },
  actions: {
    async setDictMap() {
      const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
      if (dictMap) {
        this.dictMap = dictMap
        this.isSetDict = true
      } else {
         const res = await getSimpleDictDataList()

        const rest =[
          {
              "dictType": "pay_channel_code",
              "value": "wx_pub",
              "label": "微信公众号支付",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "pay_channel_code",
              "value": "wx_lite",
              "label": "微信小程序支付",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "pay_channel_code",
              "value": "wx_app",
              "label": "微信 App 支付",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "pay_channel_code",
              "value": "wx_native",
              "label": "微信扫码支付",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "pay_channel_code",
              "value": "wx_bar",
              "label": "微信条码支付",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "pay_channel_code",
              "value": "alipay_pc",
              "label": "支付宝 PC 网站支付",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "pay_channel_code",
              "value": "alipay_wap",
              "label": "支付宝 Wap 网站支付",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "pay_channel_code",
              "value": "alipay_app",
              "label": "支付宝 App 支付",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "pay_channel_code",
              "value": "alipay_bar",
              "label": "支付宝条码支付",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "pay_channel_code",
              "value": "alipay_qr",
              "label": "支付宝扫码支付",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "pay_channel_code",
              "value": "mock",
              "label": "模拟支付",
              "colorType": "default",
              "cssClass": ""
          },
          {
              "dictType": "pay_channel_code",
              "value": "wallet",
              "label": "钱包",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "pay_notify_status",
              "value": "0",
              "label": "等待通知",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "pay_notify_status",
              "value": "10",
              "label": "通知成功",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "pay_notify_status",
              "value": "20",
              "label": "通知失败",
              "colorType": "danger",
              "cssClass": ""
          },
          {
              "dictType": "pay_notify_status",
              "value": "21",
              "label": "请求成功，但是结果失败",
              "colorType": "warning",
              "cssClass": ""
          },
          {
              "dictType": "pay_notify_status",
              "value": "22",
              "label": "请求失败",
              "colorType": "warning",
              "cssClass": ""
          },
          {
              "dictType": "pay_notify_type",
              "value": "1",
              "label": "支付单",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "pay_notify_type",
              "value": "2",
              "label": "退款单",
              "colorType": "danger",
              "cssClass": ""
          },
          {
              "dictType": "pay_order_status",
              "value": "0",
              "label": "等待支付",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "pay_order_status",
              "value": "10",
              "label": "支付成功",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "pay_order_status",
              "value": "20",
              "label": "已退款",
              "colorType": "danger",
              "cssClass": ""
          },
          {
              "dictType": "pay_order_status",
              "value": "30",
              "label": "支付关闭",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "pay_refund_status",
              "value": "0",
              "label": "等待退款",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "pay_refund_status",
              "value": "10",
              "label": "退款成功",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "pay_refund_status",
              "value": "20",
              "label": "退款失败",
              "colorType": "danger",
              "cssClass": ""
          },
          {
              "dictType": "pay_transfer_status",
              "value": "0",
              "label": "等待转账",
              "colorType": "default",
              "cssClass": ""
          },
          {
              "dictType": "pay_transfer_status",
              "value": "10",
              "label": "转账进行中",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "pay_transfer_status",
              "value": "20",
              "label": "转账成功",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "pay_transfer_status",
              "value": "30",
              "label": "转账失败",
              "colorType": "warning",
              "cssClass": ""
          },
          {
              "dictType": "pay_transfer_type",
              "value": "1",
              "label": "支付宝余额",
              "colorType": "default",
              "cssClass": ""
          },
          {
              "dictType": "pay_transfer_type",
              "value": "2",
              "label": "微信余额",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "pay_transfer_type",
              "value": "3",
              "label": "银行卡",
              "colorType": "default",
              "cssClass": ""
          },
          {
              "dictType": "pay_transfer_type",
              "value": "4",
              "label": "钱包余额",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "product_spu_status",
              "value": "0",
              "label": "仓库中",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "product_spu_status",
              "value": "-1",
              "label": "回收站",
              "colorType": "default",
              "cssClass": ""
          },
          {
              "dictType": "product_spu_status",
              "value": "1",
              "label": "销售中",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "promotion_activity_status",
              "value": "10",
              "label": "未开始",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "promotion_activity_status",
              "value": "20",
              "label": "进行中",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "promotion_activity_status",
              "value": "30",
              "label": "已结束",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "promotion_activity_status",
              "value": "40",
              "label": "已关闭",
              "colorType": "warning",
              "cssClass": ""
          },
          {
              "dictType": "promotion_banner_position",
              "value": "5",
              "label": "满减送页",
              "colorType": "warning",
              "cssClass": ""
          },
          {
              "dictType": "promotion_banner_position",
              "value": "4",
              "label": "限时折扣页",
              "colorType": "warning",
              "cssClass": ""
          },
          {
              "dictType": "promotion_banner_position",
              "value": "3",
              "label": "砍价活动页",
              "colorType": "warning",
              "cssClass": ""
          },
          {
              "dictType": "promotion_banner_position",
              "value": "2",
              "label": "秒杀活动页",
              "colorType": "warning",
              "cssClass": ""
          },
          {
              "dictType": "promotion_banner_position",
              "value": "1",
              "label": "首页",
              "colorType": "warning",
              "cssClass": ""
          },
          {
              "dictType": "promotion_bargain_record_status",
              "value": "1",
              "label": "砍价中",
              "colorType": "default",
              "cssClass": ""
          },
          {
              "dictType": "promotion_bargain_record_status",
              "value": "2",
              "label": "砍价成功",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "promotion_bargain_record_status",
              "value": "3",
              "label": "砍价失败",
              "colorType": "warning",
              "cssClass": ""
          },
          {
              "dictType": "promotion_combination_record_status",
              "value": "1",
              "label": "拼团中",
              "colorType": "",
              "cssClass": ""
          },
          {
              "dictType": "promotion_combination_record_status",
              "value": "2",
              "label": "拼团成功",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "promotion_combination_record_status",
              "value": "3",
              "label": "拼团失败",
              "colorType": "warning",
              "cssClass": ""
          },
          {
              "dictType": "promotion_condition_type",
              "value": "10",
              "label": "满 N 元",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "promotion_condition_type",
              "value": "20",
              "label": "满 N 件",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "promotion_coupon_status",
              "value": "1",
              "label": "未使用",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "promotion_coupon_status",
              "value": "2",
              "label": "已使用",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "promotion_coupon_status",
              "value": "3",
              "label": "已过期",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "promotion_coupon_take_type",
              "value": "1",
              "label": "直接领取",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "promotion_coupon_take_type",
              "value": "2",
              "label": "指定发放",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "promotion_coupon_template_validity_type",
              "value": "1",
              "label": "固定日期",
              "colorType": "default",
              "cssClass": ""
          },
          {
              "dictType": "promotion_coupon_template_validity_type",
              "value": "2",
              "label": "领取之后",
              "colorType": "default",
              "cssClass": ""
          },
          {
              "dictType": "promotion_discount_type",
              "value": "1",
              "label": "满减",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "promotion_discount_type",
              "value": "2",
              "label": "折扣",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "promotion_product_scope",
              "value": "1",
              "label": "通用劵",
              "colorType": "default",
              "cssClass": ""
          },
          {
              "dictType": "promotion_product_scope",
              "value": "2",
              "label": "商品劵",
              "colorType": "default",
              "cssClass": ""
          },
          {
              "dictType": "promotion_product_scope",
              "value": "3",
              "label": "品类劵",
              "colorType": "default",
              "cssClass": ""
          },
          {
              "dictType": "system_data_scope",
              "value": "1",
              "label": "全部数据权限",
              "colorType": "",
              "cssClass": ""
          },
          {
              "dictType": "system_data_scope",
              "value": "2",
              "label": "指定部门数据权限",
              "colorType": "",
              "cssClass": ""
          },
          {
              "dictType": "system_data_scope",
              "value": "3",
              "label": "本部门数据权限",
              "colorType": "",
              "cssClass": ""
          },
          {
              "dictType": "system_data_scope",
              "value": "4",
              "label": "本部门及以下数据权限",
              "colorType": "",
              "cssClass": ""
          },
          {
              "dictType": "system_data_scope",
              "value": "5",
              "label": "仅本人数据权限",
              "colorType": "",
              "cssClass": ""
          },
          {
              "dictType": "system_login_result",
              "value": "0",
              "label": "成功",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "system_login_result",
              "value": "10",
              "label": "账号或密码不正确",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "system_login_result",
              "value": "20",
              "label": "用户被禁用",
              "colorType": "warning",
              "cssClass": ""
          },
          {
              "dictType": "system_login_result",
              "value": "30",
              "label": "验证码不存在",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "system_login_result",
              "value": "31",
              "label": "验证码不正确",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "system_login_result",
              "value": "100",
              "label": "未知异常",
              "colorType": "danger",
              "cssClass": ""
          },
          {
              "dictType": "system_login_type",
              "value": "100",
              "label": "账号登录",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "system_login_type",
              "value": "101",
              "label": "社交登录",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "system_login_type",
              "value": "103",
              "label": "短信登录",
              "colorType": "default",
              "cssClass": ""
          },
          {
              "dictType": "system_login_type",
              "value": "200",
              "label": "主动登出",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "system_login_type",
              "value": "202",
              "label": "强制登出",
              "colorType": "danger",
              "cssClass": ""
          },
          {
              "dictType": "system_mail_send_status",
              "value": "0",
              "label": "初始化",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "system_mail_send_status",
              "value": "10",
              "label": "发送成功",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "system_mail_send_status",
              "value": "20",
              "label": "发送失败",
              "colorType": "danger",
              "cssClass": ""
          },
          {
              "dictType": "system_mail_send_status",
              "value": "30",
              "label": "不发送",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "system_menu_type",
              "value": "1",
              "label": "目录",
              "colorType": "",
              "cssClass": ""
          },
          {
              "dictType": "system_menu_type",
              "value": "2",
              "label": "菜单",
              "colorType": "",
              "cssClass": ""
          },
          {
              "dictType": "system_menu_type",
              "value": "3",
              "label": "按钮",
              "colorType": "",
              "cssClass": ""
          },
          {
              "dictType": "system_notice_type",
              "value": "1",
              "label": "通知",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "system_notice_type",
              "value": "2",
              "label": "公告",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "system_notify_template_type",
              "value": "1",
              "label": "通知公告",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "system_notify_template_type",
              "value": "2",
              "label": "系统消息",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "system_oauth2_grant_type",
              "value": "password",
              "label": "password",
              "colorType": "default",
              "cssClass": ""
          },
          {
              "dictType": "system_oauth2_grant_type",
              "value": "authorization_code",
              "label": "authorization_code",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "system_oauth2_grant_type",
              "value": "implicit",
              "label": "implicit",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "system_oauth2_grant_type",
              "value": "client_credentials",
              "label": "client_credentials",
              "colorType": "default",
              "cssClass": ""
          },
          {
              "dictType": "system_oauth2_grant_type",
              "value": "refresh_token",
              "label": "refresh_token",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "system_role_type",
              "value": "1",
              "label": "内置",
              "colorType": "danger",
              "cssClass": ""
          },
          {
              "dictType": "system_role_type",
              "value": "2",
              "label": "自定义",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "system_sms_channel_code",
              "value": "DEBUG_DING_TALK",
              "label": "调试(钉钉)",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "system_sms_channel_code",
              "value": "ALIYUN",
              "label": "阿里云",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "system_sms_receive_status",
              "value": "0",
              "label": "等待结果",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "system_sms_receive_status",
              "value": "10",
              "label": "接收成功",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "system_sms_receive_status",
              "value": "20",
              "label": "接收失败",
              "colorType": "danger",
              "cssClass": ""
          },
          {
              "dictType": "system_sms_send_status",
              "value": "0",
              "label": "初始化",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "system_sms_send_status",
              "value": "10",
              "label": "发送成功",
              "colorType": "success",
              "cssClass": ""
          },
          {
              "dictType": "system_sms_send_status",
              "value": "20",
              "label": "发送失败",
              "colorType": "danger",
              "cssClass": ""
          },
          {
              "dictType": "system_sms_send_status",
              "value": "30",
              "label": "不发送",
              "colorType": "info",
              "cssClass": ""
          },
          {
              "dictType": "system_sms_template_type",
              "value": "3",
              "label": "营销",
              "colorType": "danger",
              "cssClass": ""
          },
          {
              "dictType": "system_sms_template_type",
              "value": "1",
              "label": "验证码",
              "colorType": "warning",
              "cssClass": ""
          },
          {
              "dictType": "system_sms_template_type",
              "value": "2",
              "label": "通知",
              "colorType": "primary",
              "cssClass": ""
          },
          {
              "dictType": "system_social_type",
              "value": "10",
              "label": "Gitee",
              "colorType": "",
              "cssClass": ""
          },
          {
              "dictType": "system_social_type",
              "value": "20",
              "label": "钉钉",
              "colorType": "",
              "cssClass": ""
          },
          {
              "dictType": "system_social_type",
              "value": "30",
              "label": "企业微信",
              "colorType": "",
              "cssClass": ""
          },
          {
              "dictType": "system_social_type",
              "value": "31",
              "label": "微信公众平台",
              "colorType": "",
              "cssClass": ""
          },
          {
              "dictType": "system_social_type",
              "value": "32",
              "label": "微信开放平台",
              "colorType": "",
              "cssClass": ""
          },
          {
              "dictType": "system_social_type",
              "value": "34",
              "label": "微信小程序",
              "colorType": "",
              "cssClass": ""
          },
          {
              "dictType": "system_user_sex",
              "value": "1",
              "label": "男",
              "colorType": "default",
              "cssClass": "A"
          },
          {
              "dictType": "system_user_sex",
              "value": "2",
              "label": "女",
              "colorType": "success",
              "cssClass": ""
          }
      ]
        // console.log("请求字典返回》》》",res);
        // 设置数据
        const dictDataMap = new Map<string, any>()
        res.forEach((dictData: DictDataVO) => {
          // 获得 dictType 层级
          const enumValueObj = dictDataMap[dictData.dictType]
          if (!enumValueObj) {
            dictDataMap[dictData.dictType] = []
          }
          // 处理 dictValue 层级
          dictDataMap[dictData.dictType].push({
            value: dictData.value,
            label: dictData.label,
            colorType: dictData.colorType,
            cssClass: dictData.cssClass
          })
        })
        this.dictMap = dictDataMap
        this.isSetDict = true
        wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期
      }
    },
    getDictByType(type: string) {
     //   debugger
      if (!this.isSetDict) {
        this.setDictMap()
      }
      return this.dictMap[type]
    },
    async resetDict() {
      wsCache.delete(CACHE_KEY.DICT_CACHE)
      const res = await getSimpleDictDataList()
      // 设置数据
      const dictDataMap = new Map<string, any>()
      res.forEach((dictData: DictDataVO) => {
        // 获得 dictType 层级
        const enumValueObj = dictDataMap[dictData.dictType]
        if (!enumValueObj) {
          dictDataMap[dictData.dictType] = []
        }
        // 处理 dictValue 层级
        dictDataMap[dictData.dictType].push({
          value: dictData.value,
          label: dictData.label,
          colorType: dictData.colorType,
          cssClass: dictData.cssClass
        })
      })
      this.dictMap = dictDataMap
      this.isSetDict = true
      wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期
    }
  }
})

export const useDictStoreWithOut = () => {
  return useDictStore(store)
}
